#include"myheadd.h"

Friend *find23(Friend *friend_list,char *user_name)
{
    Friend  *fi=NULL;
    list_for_each_entry(fi,&friend_list->list,list)
    {
        if((strcmp(fi->user_name,user_name)==0))
        return fi;
    }
    return NULL;
}
void *send_signature_broadcast(void *arg)
{
    if(!arg) 
    return NULL;
    char *msg = (char *)arg;
    int sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd < 0)
    {
        perror("创建签名广播套接字失败\n");
        free(msg);
        return NULL;
    }

    int on=1;
    if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on)) < 0)
    {
        perror("设置广播失败\n");
        close(sockfd);
        free(msg);
        return NULL;
    }
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(54321);
    addr.sin_addr.s_addr = inet_addr("192.168.4.255");

    sendto(sockfd,msg,strlen(msg),0,(struct sockaddr *)&addr ,sizeof(addr));
   

    close(sockfd);
    free(msg);
    return NULL;
}
void aaa(char *user_name)
{
    pthread_mutex_lock(&list_mutex);
    printf("您当前的个人资料如下👇\n");
    Friend *tmp = NULL;
    list_for_each_entry(tmp,&friend_list->list,list)
    {
        if(strcmp(tmp->user_name,user_name)==0)
        {
            printf("\n您的用户名是:%s\t 您的ip是:%s\n 您的端口号是：%hu\t  您的私聊端口是:%hu\n  您的文件端口是:%hu\t  您的个性签名是:%s\n",
            tmp->user_name,tmp->ip,tmp->port,tmp->private_port,tmp->file_port,tmp->user_qm);
            break;
        }
    }
    pthread_mutex_unlock(&list_mutex);

    printf("=========若要修改自己个性签名请按8==========\n");
    printf("=========若要查询好友个性签名请按9==========\n");
    printf("请输入您的操作码:");
    int i=0;
    scanf("%d",&i);
    while(getchar() != '\n');
    switch(i)
    {
        case  8:
        {       printf("请输入新的个性签名:");
                char arr[1024];
                if(scanf("%s",arr)!=1)
                {
                    printf("个性签名输入错误\n");
                    while (getchar() != '\n');
                    break;
                }
                while (getchar() != '\n');

                pthread_mutex_lock(&list_mutex);
                Friend  *fi=NULL;
                fi=find23(friend_list,user_name);
                if(fi)
                {
                    strncpy(fi->user_qm,arr,sizeof(fi->user_qm)-1);
                    printf("===个性签名修改成功===\n");
                    char *brodacast_msg = malloc(1024+20+2);
                if(brodacast_msg)
                {
                    snprintf(brodacast_msg,1024+20+2,"%s:2:%s",user_name,arr);
                    pthread_t tid;
                    if(pthread_create(&tid,NULL,send_signature_broadcast,brodacast_msg))
                        {
                            perror("创建签名广播线程失败");
                            free(brodacast_msg);
                        }
                    else
                        {
                            pthread_detach(tid);
                        }
                }
                    pthread_mutex_unlock(&list_mutex);
                }
                
               break;
        }
                break;
        case 9: 
                printf("请输入你要查询好友的用户名:");
                char user_name2[20];
                if(scanf("%s",user_name2)!=1)
                {
                    printf("好友用户名输入错误\n");
                    while (getchar() != '\n');
                    break;
                }
                while (getchar() != '\n');
                Friend  *tmp=NULL;
                tmp=find23(friend_list,user_name2);
                printf("\n好友用户名是:%s\t 好友ip是:%s\n 好友端口号是：%hu\t  好友私聊端口是:%hu\n  好友文件端口是:%hu\t  好友个性签名是:%s\n",
                     tmp->user_name,tmp->ip,tmp->port,tmp->private_port,tmp->file_port,tmp->user_qm);
                 break;
        default:
                printf("无效输入\n");
    } 
}